
package orca;

import java.awt.Graphics2D;

public class OrcaPane extends OrcaComponent
{
	public static final int VERTICAL = 0;
	public static final int HORIZONTAL = 1;

	private final int orientation;

	public int maxSize = Integer.MAX_VALUE;

	private OrcaComponent son = null;

	public OrcaPane(int orientation)
	{
		this.orientation = orientation;
	}

	public OrcaPane()
	{
		this(OrcaPane.VERTICAL);
	}

	public int getOrientation()
	{
		return this.orientation;
	}

	public void add(OrcaComponent component)
	{
		OrcaComponent temp1 = null;
		OrcaComponent temp2 = null;

		if (this.son == null)
		{
			this.son = component;
		}
		else
		{
			temp1 = this.son;
			temp2 = this.son.brother;

			while (temp2 != null)
			{
				temp1 = temp2;
				temp2 = temp1.brother;
			}

			temp1.brother = component;
		}
	}

	@Override
	public void mouseClicked(int button, int x, int y)
	{
		OrcaComponent temp;

		if (this.frame.contains(x, y))
		{
			temp = this.son;

			while (temp != null)
			{
				temp.mouseClicked(button, x, y);
				temp = temp.brother;
			}
		}
	}

	@Override
	public void mousePressed(int button, int x, int y)
	{
		OrcaComponent temp;

		if (this.frame.contains(x, y))
		{
			temp = this.son;

			while (temp != null)
			{
				temp.mousePressed(button, x, y);
				temp = temp.brother;
			}
		}
	}

	@Override
	public void mouseReleased(int button, int x, int y)
	{
		OrcaComponent temp;

		if (this.frame.contains(x, y))
		{
			temp = this.son;

			while (temp != null)
			{
				temp.mouseReleased(button, x, y);
				temp = temp.brother;
			}
		}
	}

	@Override
	void refresh(Graphics2D g2d)
	{
		OrcaComponent temp;

		System.out.println(this + ": drawing");

		g2d.setColor(this.getBackground());
		g2d.fill(this.frame);

		g2d.setColor(this.getForeground());
		g2d.draw(this.frame);

		temp = this.son;
		while (temp != null)
		{
			temp.refresh(g2d);
			temp = temp.brother;
		}
	}

	@Override
	void setBounds(int x, int y, int width, int height)
	{
		OrcaComponent sons;
		int divCounts;
		int limit;
		int jump;
		int effectiveJump;
		int restant;
		int diffSize;
		int maxSize;
		int location1;
		int location2;
		int location3;

		this.frame.setBounds(x, y, width, height);

		sons = this.son;
		if (sons != null)
		{
			divCounts = sons.getNbBrothers();

			limit = y + height;
			restant = 0;

			if (this.orientation == VERTICAL)
			{
				limit = y + height;
				jump = height / divCounts;
				location1 = y;
				location2 = location1 + jump;

				do
				{
					location3 = location2 + jump;

					// Gérer les cas ou il resterait de de l'espace a cause de la division
					if (location3 > limit)
					{
						jump = limit - location1;
					}
					

					sons.setBounds(x, location1, width, jump);

					location1 = location2;
					location2 = location3;

					sons = sons.brother;
				}
				while (sons != null);
			}
			else
			{
				limit = x + width;
				jump = width / divCounts;
				location1 = x;
				location2 = location1 + jump;

				do
				{
					location3 = location2 + jump;

					if (location3 > limit)
					{
						jump = limit - location1;
					}
					

					sons.setBounds(location1 , y, jump, height);

					location1 = location2;
					location2 = location3;

					sons = sons.brother;
				}
				while (sons != null);
			}
		}
	}

}
